from django.shortcuts import render,HttpResponse
from .models import Article
# Create your views here.

def index(request):
    return HttpResponse('Django is working!<br><a href="blog/">进入我的博客</a><br><a href="chattingroom/">进入聊天室</a>')

def blog_index(request):
    return render(request,'blog/index.html')

def blog_list(request):
    articles = Article.objects.all()
    return render(request,'blog/blog_list.html',{'articles':articles})

def blog_edit(request,atc_id):
    if int(atc_id) == 0:
        return  render(request,'blog/blog_edit.html')
    article = Article.objects.get(pk=atc_id)
    return render(request, 'blog/blog_edit.html',{'article':article})

def edit_action(request):
    title = request.POST.get('title','TITLE')  #title = request.POST['title']
    content = request.POST.get('content','CONTENT')
    atc_id = request.POST.get('atc_id',0)
    if int(atc_id) == 0:
        Article.objects.create(title=title,content=content)
        articles = Article.objects.all()
        return render(request, 'blog/blog_list.html', {'articles': articles})
    else:
        article = Article.objects.get(pk=atc_id)
        article.title = title
        article.content = content
        article.save()
        return render(request, 'blog/blog_page.html', {'article': article})


def blog_page1(request):
    article = Article.objects.get(pk=1)
    return render(request,'blog/blog_page1.html',{'article':article})

def blog_page(request,atc_id):
    article = Article.objects.get(pk=atc_id)
    return render(request,'blog/blog_page.html',{'article':article})